#!/bin/bash
#
# alg2docker 0.2
#
# Convert an Algernon application (.alg or .zip) to a Dockerfile
#

# default settings
filename=hello.alg
newfile=Dockerfile
name="$(whoami)"
email="$EMAIL"
force="false"

if [[ $1 == "" || $1 == "-h" || $1 == "--help" ]]; then
  echo 'alg2docker 0.2'
  echo
  echo 'Convert an Algernon application (.alg or .zip) to a Dockerfile'
  echo
  echo 'Usage:'
  echo '    alg2docker [-f] ALGFILE [DOCKERFILE] [NAME] [EMAIL]'
  echo
  echo 'Example:'
  echo "    alg2docker hello.alg Dockerfile '$name' '$email'"
  echo
  exit 1
fi

if [[ $1 == "-f" ]]; then
  force="true"
  shift
fi

if [[ $1 != "" ]]; then filename="$1"; fi
if [[ $2 != "" ]]; then newfile="$2"; fi
if [[ $3 != "" ]]; then name="$3"; fi
if [[ $4 != "" ]]; then email="$4"; fi

if [[ -e $newfile && $force == "false" ]]; then
  echo "ERROR: file already exists: $newfile"
  echo "Use -f as the first argument to overwrite."
  exit 1
fi

cat <<EOF > "$newfile"
# Dockerfile for serving an Algernon application as
# * HTTP on port 80
# * HTTPS+HTTP/2 on port 443

FROM golang:alpine
MAINTAINER !NAME <!EMAIL>

RUN apk update && apk add bash curl git mercurial go && rm -rf /var/cache/apk/*
RUN mkdir -p /go/src /go/bin && chmod -R 777 /go
ENV GOPATH /go
ENV PATH /go/bin/:$PATH
ENV CGO_ENABLED=0
RUN go get github.com/xyproto/algernon

# Add the .alg file
ADD !FILENAME !FILENAME

# Add the configuration directory
# Should contain the HTTPS keys (cert.pem and key.pem)
VOLUME /etc/algernon

# Serve the .alg application, in a static, but fast, way
CMD ["algernon", "-c", "--domain", "--server", "--cachesize", "67108864", "--prod", "--cert", "/etc/algernon/cert.pem", "--key", "/etc/algernon/key.pem", "!FILENAME"]
EXPOSE 80 443

EOF

# Configure the Dockerfile
sed "s/\!NAME/$name/g" -i "$newfile"
sed "s/\!EMAIL/$email/g" -i "$newfile"
sed "s/\!FILENAME/$filename/g" -i "$newfile"
